/**
 * redis数据库存取操作集
 *
 * @Author bian
 * @CreateDate 2018.3.1
 */
module.exports = (prefix) => {      // 修改成函数，可自定义prefix
    var redis = require("redis");
    var Promise = require("bluebird");

    Promise.promisifyAll(redis.RedisClient.prototype);
    Promise.promisifyAll(redis.Multi.prototype);

    var client = redis.createClient();
    var commands = ["set", "setex", "get","getAsync","setexAsync"];

    commands.forEach( function (cmd)  {
        var oldCmd = `_${cmd}`;
        client[oldCmd] = client[cmd];
        client[cmd] = function (key, arg, cb) {
            arguments[0] = `${prefix}/${arguments[0]}`;
            return client[oldCmd].apply(this, arguments);
        };
    });

    client.jsetex = (key,expire,value) => client.setexAsync(key,expire,JSON.stringify(value));
    client.jget = (key) => {
        return client.getAsync(key).then((res) => {
            return JSON.parse(res);
        });
    };

    return client;
};